package org.adw.launcher2.ld.secret.ui.adapter;

import java.util.LinkedList;
import java.util.List;

import org.adw.launcher2.ld.secret.dom.contact.UserInfo;

import org.adw.launcher2.R;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MyContactAdapter extends ArrayAdapter<UserInfo> {

	private Context context;
	private LayoutInflater inflater;
	public static List<UserInfo> listToHide = null;
	
	public MyContactAdapter(Context context, int textViewResourceId,
			List<UserInfo> list) {
		super(context, textViewResourceId, list);
		this.context = context;
		inflater = LayoutInflater.from(context);
		listToHide = new LinkedList<UserInfo>();
		
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
//		if(convertView==null){
			convertView = inflater.inflate(R.layout.contact_info_item, null);
			holder = new ViewHolder();
			holder.txtName = (TextView) convertView.findViewById(R.id.txtName);
			holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
			
			convertView.setTag(holder);
//		}else{
//			holder = (ViewHolder) convertView.getTag();
//		}
		final UserInfo myContact = getItem(position);
		holder.txtName.setText(myContact.getUserName());
		holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					if(!listToHide.contains(myContact)){
						listToHide.add(getItem(position));
					}
				}else{
					listToHide.remove(getItem(position));
				}
			}
		});
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				Intent intent = new Intent
				(Intent.ACTION_VIEW, Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, ""+myContact.getUserId()));

				context.startActivity(intent);
				
			}
		});
		return convertView;
	}

	static class ViewHolder {
		TextView txtName;
		CheckBox checkBox;
	}
}
